<?php

namespace Addons\NearBy\Model;
use Home\Model\WeixinModel;

/**
 * NearBy的微信模型
 */
class WeixinAddonModel extends WeixinModel {

    function reply($dataArr, $keywordArr = array()) {
        $config = getAddonConfig ( 'NearBy' ); // 获取后台插件的配置参数
    }

    // 关注公众号事件
    public function subscribe() {
        return true;
    }

    // 取消关注公众号事件
    public function unsubscribe() {
        return true;
    }

    // 扫描带参数二维码事件
    public function scan() {
        return true;
    }

    // 上报地理位置事件
    public function location($dataArr) {
        if (strtolower($dataArr['MsgType']) != 'location') {
            return;
        }
        $config = getAddonConfig ( 'NearBy' ); // 获取后台插件的配置参数
        $location_X = $dataArr['Location_X']; // 纬度
        $location_Y = $dataArr['Location_Y']; // 经度
        if ($config['url'] && $config['url'] != '#') {
            $res = file_get_contents($config['url'].'&location_x='.$location_X.'&location_y='.$location_Y);
            $res ? $res = json_decode($res) : false;
        } else {
            $res = file_get_contents('www.szjlxh.com/weiphp/Addons/Weishop/shop/admin/index.php?route=catalog/product/getnearbyshops&location_x='.$location_X.'&location_y='.$location_Y);
            $res ? $res = json_decode($res) : false;
        }
        if (!$res) {
            $this->replyText("附近暂无打折商家，我们正在火速招商中，敬请期待！");
            die();
        } else {
            $isNearest = true;
            foreach ($res as $shop) {
                if ($isNearest) {
                    $articles[] = array (
                        'Title' => '最近:'.$shop->name.' ('.$shop->distance.')',
                        'Description' => '此店铺距离你'.$shop->distance.',具体地址:'.$shop->location,
                        'PicUrl' => $shop->image_url ? $shop->image_url : 'http://www.szjlxh.com/weiphp/Addons/Weishop/shop/image/catalog/F81756BE-4773-4286-8DEA-BB6B0FB30518.jpeg',
                        'Url' => $shop->shop_url
                    );
                    $isNearest = false;
                } else {
                    $articles[] = array (
                        'Title' => $shop->name.' ('.$shop->distance.')',
                        'Description' => '此店铺距离你'.$shop->distance.','.'具体地址:'.$shop->location,
                        'PicUrl' => $shop->image_url ? $shop->image_url : 'http://www.szjlxh.com/weiphp/Addons/Weishop/shop/image/catalog/F81756BE-4773-4286-8DEA-BB6B0FB30518.jpeg',
                        'Url' => $shop->shop_url
                    );
                }
            }
            $articles ? $this->replyNews($articles) : $this->replyText("服务器提出了一个问题，程序猿正在努力的解答中...");
            die();
        }
    }

    // 自定义菜单事件
    public function click() {
        return true;
    }
}

